package com.crawler.harbour.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.crawler.harbour.dto.QueryDTO;
import com.crawler.harbour.model.ShipmentNew;
import com.crawler.harbour.service.IShipmentNewService;
import com.crawler.harbour.utils.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author pcy
 **/
@RestController
@RequestMapping(value = "shipment/")
public class ShipmentNewController {
    
    @Autowired
    IShipmentNewService iShipmentNewService;

    
    /**
     * 厦门船期查询
     * @param page
     * @param queryDTO
     * @return
     */
    
    @RequestMapping(value="xm/search",method = RequestMethod.GET)
    public Object getShipment(Page<ShipmentNew> page,QueryDTO queryDTO) {
    	  Wrapper<ShipmentNew> wrapper =new EntityWrapper<>();
    	  wrapper.ne("ship_name","");
    	  wrapper.notIn("port_no", Lists.newArrayList("FQC","FIC","HYG"));
    	  if(StringUtils.isNotBlank(queryDTO.getPortNo())){
    		  wrapper.and("port_no={0}",queryDTO.getPortNo());
          }
          if(StringUtils.isNotBlank(queryDTO.getShipName())){
              wrapper.and("ship_name={0}",queryDTO.getShipName());
          }
    	  if(StringUtils.isNotBlank(queryDTO.getShipTimer())){
              wrapper.and("ship_timer={0}",queryDTO.getShipTimer());
          }else if(StringUtils.isNotBlank(queryDTO.getShipTimerText())){
        	  wrapper.like("ship_timer", queryDTO.getShipTimerText());
          }
    	  wrapper.and().isNotNull("ship_name");
    	  if(StringUtils.isNotBlank(queryDTO.getKeyword())){
    		  wrapper.andNew()
    		  .like("ship_name", queryDTO.getKeyword()).or()
    		  .like("ship_timer", queryDTO.getKeyword());
          }
        return iShipmentNewService.selectPage(page,wrapper);
    }
    
    /**
     * 福州船期查询
     * @param page
     * @param queryDTO
     * @return
     */
    @RequestMapping(value="fz/search",method = RequestMethod.GET)
    public Object getFzShipment(Page<ShipmentNew> page,QueryDTO queryDTO) {
    	  Wrapper<ShipmentNew> wrapper =new EntityWrapper<>();
    	  wrapper.in("port_no", Lists.newArrayList("FQC","FIC","HYG"));
    	  wrapper.ne("ship_name","");
    	  if(StringUtils.isNotBlank(queryDTO.getPortNo())){
    		  wrapper.and("port_no={0}",queryDTO.getPortNo());
          }
          if(StringUtils.isNotBlank(queryDTO.getShipName())){
              wrapper.and("ship_name={0}",queryDTO.getShipName());
          }
    	  if(StringUtils.isNotBlank(queryDTO.getShipTimer())){
              wrapper.and("ship_timer={0}",queryDTO.getShipTimer());
          }else if(StringUtils.isNotBlank(queryDTO.getShipTimerText())){
        	  wrapper.like("ship_timer", queryDTO.getShipTimerText());
          }
    	  wrapper.and().isNotNull("ship_name");
    	  if(StringUtils.isNotBlank(queryDTO.getKeyword())){
    		  wrapper.andNew()
    		  .like("ship_name", queryDTO.getKeyword()).or()
    		  .like("ship_timer", queryDTO.getKeyword());
          }
        return iShipmentNewService.selectPage(page,wrapper);
    }
    /**
     * 福州船名查询
     * @param page
     * @param queryDTO
     * @return
     */
    @RequestMapping(value = "fz/name")
    public Object getShipName(Page<Map<String,Object>> page,QueryDTO queryDTO) {
    	long offset = (page.getCurrent()-1)*page.getSize();
    	Map<String,Object> params = Maps.newHashMap();
    	params.put("offset", offset<0?0:offset);
        params.put("limit", page.getSize()<0?20:page.getSize());
    	params.put("ship_name", queryDTO.getShipName());   
    	params.put("ship_cn_name", queryDTO.getShipCnName()); 
    	params.put("matouRangeSql", "in('FQC','FIC','HYG')");
    	List<Map<String,Object>> shipNameResult=iShipmentNewService.selectShipNameList(params);
    	page.setRecords(shipNameResult);
    	params.put("offset", 0);
    	page.setTotal(iShipmentNewService.countShipNameList(params));
        return page;
    }
    /**
     * 厦门船名查询
     * @param page
     * @param queryDTO
     * @return
     */
    @RequestMapping(value = "xm/name")
    public Object getXmShipName(Page<Map<String,Object>> page,QueryDTO queryDTO) {
    	long offset = (page.getCurrent()-1)*page.getSize();
    	Map<String,Object> params = Maps.newHashMap();
    	params.put("offset", offset<0?0:offset);
        params.put("limit", page.getSize()<0?20:page.getSize());
    	params.put("ship_name", queryDTO.getShipName());    
    	params.put("ship_cn_name", queryDTO.getShipCnName());    
    	params.put("matouRangeSql", "not in('FQC','FIC','HYG')");
    	
    	List<Map<String,Object>> shipNameResult=iShipmentNewService.selectShipNameList(params);
    	page.setRecords(shipNameResult);
    	params.put("offset", 0);
    	page.setTotal(iShipmentNewService.countShipNameList(params));
        return page;
    }
}
